package de.bht.cge.flightsim.game;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.media.opengl.GL2GL3;

import de.bht.jvr.core.AttributeCloud;
import de.bht.jvr.core.ShaderMaterial;
import de.bht.jvr.core.ShaderProgram;
import de.bht.jvr.core.ShapeNode;
import de.bht.jvr.core.Texture2D;
import de.bht.jvr.core.Transform;
import de.bht.jvr.core.attributes.AttributeVector3;
import de.bht.jvr.core.attributes.AttributeVector4;
import de.bht.jvr.core.uniforms.UniformBool;
import de.bht.jvr.core.uniforms.UniformFloat;
import de.bht.jvr.math.Vector3;
import de.bht.jvr.math.Vector4;

public class Cloud extends Base
{
	private int count;
	private float alpha;
	private ShaderMaterial mat;
	
	 public Cloud(float scale)
	 {
		 super(scale,"Cloud");
		 this.count = 10000;
		 this.alpha = 0.05f;
		 try
			{
				this.Build();
			}
			catch (Exception e)
			{			
				e.printStackTrace();
			}
	 }	
	 
	 @Override
	 public void Build() throws Exception
	 {
		 super.Build();
		 
        ShapeNode cloudShape = new ShapeNode();
        
        ShaderProgram program = 
            new ShaderProgram(
                new File("shaders/Cloud/sprites.vs"), 
                new File("shaders/Cloud/sprites.gs"), 
                new File("shaders/Cloud/sprites.fs"));
                        
        program.setParameter(GL2GL3.GL_GEOMETRY_INPUT_TYPE, GL2GL3.GL_POINTS);
        program.setParameter(GL2GL3.GL_GEOMETRY_OUTPUT_TYPE, GL2GL3.GL_TRIANGLE_STRIP);
        program.setParameter(GL2GL3.GL_GEOMETRY_VERTICES_OUT, 4);

        this.mat = new ShaderMaterial("CLOUD", program);        
        
        mat.setTexture("CLOUD)", "jvr_Texture0", new Texture2D(new File("textures/Cloud/cloud3.png")));
        mat.setUniform("CLOUD", "jvr_UseTexture0", new UniformBool(true));
        mat.setUniform("CLOUD", "cloud_alpha", new UniformFloat(this.getAlpha()));
        
        
        cloudShape.setMaterial(mat);
 
        
        AttributeCloud cloud = new AttributeCloud(this.getCount(), GL2GL3.GL_POINTS); 
        List<Vector4> vertices = new ArrayList<Vector4>();
        List<Vector3> colors = new ArrayList<Vector3>();
        for(int i=0; i<this.getCount(); i++)
        {
            vertices.add(new Vector4(80*(float)(Math.random())-40, 80*(float)(Math.random())-40, 80*(float)(Math.random())-40, 1));
            colors.add(new Vector3((float)Math.random(), (float)Math.random(), (float)Math.random()));
        }
        cloud.setAttribute("jvr_Vertex", new AttributeVector4(vertices));
        cloud.setAttribute("jvr_Color", new AttributeVector3(colors));
        vertices.clear();
        colors.clear();
        cloudShape.setGeometry(cloud);
        cloudShape.setTransform(Transform.scale(5.0f));
        
        this.getNode().addChildNode(cloudShape);
	 }

	public int getCount()
	{
		return count;
	}

	public void setCount(int count)
	{
		this.count = count;
	}

	public float getAlpha()
	{
		return alpha;
	}

	public void setAlpha(float alpha)
	{
		this.alpha = alpha;
		mat.setUniform("AMBIENT", "cloud_alpha", new UniformFloat(this.getAlpha()));
	}
	 
	 
	 
	
}
